$('.item')
	.bind('dragstart', function (evt) {
		evt.dataTransfer.setData('text', this.id);
		$('h2').fadeIn('slow');
		$(this).css({"opacity":"0.2","filter":"alpha(opacity=20)"});
	}).bind('dragend', function (evt) {
		evt.dataTransfer.setData('text', this.id);
		$('h2').fadeIn('slow');
		$(this).css({"opacity":"1.0","filter":"alpha(opacity=100)"});
	})
	.draggable();
$("a").click(function(event) {
  event.preventDefault();
});

$('#cart')
	.bind('dragover', function (evt) {
		evt.preventDefault();
		//alert("dragover");
		
	})
	.bind('dragenter', function (evt) {
		evt.preventDefault();
	})
	.bind('drop', function (evt) {
		var id = evt.dataTransfer.getData('text'),
			item = $('#' + id),
			cartList = $("#cart ul"),
			total = $("#total span"),
			price = $('p:eq(1) span', item).text(),
			prevCartItem = null,
			notInCart = (function () {
				var lis = $('li', cartList),
					len = lis.length,
					i;

				for (i = 0; i < len; i++ ) {
					var temp = $(lis[i]);
					if (temp.data('id') === id) {
						prevCartItem = temp;
						return false;
					}
				}
				return true;
			} ()),
			quantLeftEl, quantBoughtEl, quantLeft;

		$("h2").fadeOut('fast');

		if (notInCart) {
			prevCartItem = $('<li />', {
				
				data : { id : id }
			}).prepend($('<span />', {
				'class' : 'q',
				text : $('p:first', item).text()
			})).attr("id",id).appendTo(cartList);
		}

		quantLeftEl = $('p:last span', item);
		quantLeft   = parseInt(quantLeftEl.text(), 10) - 1;
		quantBoughtEl = $('.quantity', prevCartItem);
		quantBoughtEl.text(parseInt(quantBoughtEl.text(), 10) + 1);
		quantLeftEl.text(quantLeft);

		
			item.hide();
		

		total.text((parseFloat(total.text(), 10) + parseFloat(price.split('$')[1])).toFixed(2));

		evt.stopPropagation();
		return false;
	});

$("#clear").bind('click', function (evt) {
		var as = $('#ll  li:first' ).attr('id');
		$("#"+as).show();
		$('#ll  li:first' ).remove();
		
	});
	
	
